var eduEditState;
var delEducationAry;

function initEducation() {
	var editEducation = function() {
		/* Get the position of the current data from the node */
		var aPos = educationTable.fnGetPosition(this);
		/* Get the data array for this row */
		var aData = educationTable.fnGetData(aPos[0]);
		if (aPos[1] != 0) {
			setEduDialogField(aData[1], aData[2], aData[3], aData[4], aData[5]);
			addEducationDialog.dialogObj.dialog("open");
			eduEditState = "modify";
		}
	}

	/* Draw Table */
	$.getJSON("/hnl/LoadEducation.do?talentID=" + tid, function(data) {
		educationTable = updateTableTemplate(data, "#educationTb",
				"/hnl/template/educationTableTemplate.jsp",
				"#educationListTemplate", true, editEducation, "checkboxAll");
		educationTable.fnSetColumnVis(5, false);
	});

	var educationWarningDialog = new HnlDialogObj(
			"#dialog-no-education-selected");
	educationWarningDialog.buildDialog();
	educationWarningDialog.addDialogFunctions();

		
	var requiredMissingWarningDialog = new HnlDialogObj(
			"#dialog-education-fields-required");
	requiredMissingWarningDialog.buildDialog();
	requiredMissingWarningDialog.addDialogFunctions();

	var addEducationDialog = new HnlDialogObj("#dialog-education");
	addEducationDialog.height = 300;
	addEducationDialog.width = 700;
	addEducationDialog.buildDialog();
	addEducationDialog.addDialogFunctions(function() {
		if (hasInvalidInput_edu()) {	
			requiredMissingWarningDialog.dialogObj.dialog("open");
			return false;
		} else {
			addEducationDialog.dialogObj.dialog("close");
		}
		$.ajax({
			type : "POST",
			url : "/hnl/AddEducation.do?eduEditState=" + eduEditState,
			data : {
				talentID : tid,
				educationID : $("#educationID").val(),
				degree : $("#degree").val(),
				duration : $("#duration").val(),
				major : $("#major").val(),
				school : $("#school").val()
			},
			dataType : "json",
			success : function(data) {
				educationTable = updateTableTemplate(data, "#educationTb",
						"/hnl/template/educationTableTemplate.jsp",
						"#educationListTemplate", true, editEducation, "checkboxAll");
				educationTable.fnSetColumnVis(5, false);
			}
		});
	});

	var delEducationDialog = new HnlDialogObj("#dialog-deleteEducation");
	delEducationDialog.buildDialog();
	delEducationDialog.addDialogFunctions(function() {
		$(this).dialog("close");
		var eduAry = [];
		$("#educationListTemplate input:checkbox[name='tableCheckbox']").each(
				function() {
					if ($(this).is(":checked")) {
						eduAry.push($(this).val());
					}
				});
		$.ajax({
			type : "POST",
			url : "/hnl/DeleteEducation.do",
			data : {
				talentID : tid,
				deleteList : eduAry.join(",")
			},
			dataType : "json",
			success : function(data) {
				educationTable = updateTableTemplate(data, "#educationTb",
						"/hnl/template/educationTableTemplate.jsp",
						"#educationListTemplate", true, editEducation, "checkboxAll");
				educationTable.fnSetColumnVis(5, false);
			}
		});
	})

	$("#addEducation").click(function() {
		setEduDialogField("", "", "", "", "");
		addEducationDialog.dialogObj.dialog("open");
		eduEditState = "new";
	});

	$("#delEducation").click(
			function() {
				delEducationAry = [];
				var delEducationAry = getCheckedArray("#educationListTemplate",
						"tableCheckbox");
				if (delEducationAry.length == 0) {
					educationWarningDialog.dialogObj.dialog("open");
					return false;
				} else {
					delEducationDialog.dialogObj.dialog("open");
				}
			});
}

function hasInvalidInput_edu() {
	var edudegree = $.trim($("#degree").val());
	var eduschool = $.trim($("#school").val());
	
	if (edudegree == "" || eduschool == "") {		
		return true;
	}
	return false;
}

function setEduDialogField(degree, school, major, major, educationID) {
	$("#degree").attr("value", degree);
	$("#school").attr("value", school);
	$("#major").attr("value", major);
	$("#duration").attr("value", major);
	$("#educationID").attr("value", educationID);
}